{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Make necessary import\n",
    "\n",
    "import os\n",
    "import warnings\n",
    "from xyzspaces.datasets import get_countries_data\n",
    "import xyzspaces"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert alert-block alert-warning\">\n",
    "<b>Warning:</b> Before running below cells please make sure you have XYZ Token to interact with xyzspaces. \n",
    "                Please see README.md in notebooks folder for more info on XYZ_TOKEN\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Make a XYZ object\n",
    "\n",
    "try:\n",
    "    xyz_token = os.environ[\"XYZ_TOKEN\"]\n",
    "except KeyError:\n",
    "    xyz_token = \"MY-FANCY-XYZ-TOKEN\"\n",
    "    if xyz_token == \"MY-FANCY-XYZ-TOKEN\":\n",
    "        warnings.warn(\n",
    "            \"Please either set your actual token to env variable XYZ_TOKEN or \"\n",
    "            \"just assign value of your actual token to variable xyz_token above.\"\n",
    "\n",
    "        )\n",
    "xyz = xyzspaces.XYZ(credentials=xyz_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# List the space available\n",
    "xyz.spaces.list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a new space\n",
    "\n",
    "title = \"Testing xyzspaces\"\n",
    "description = \"Temporary space containing countries data.\"\n",
    "\n",
    "space = xyz.spaces.new(title=title, description=description)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "space.info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "space.isshared()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read from a existing space id\n",
    "\n",
    "space = xyz.spaces.from_id(space.info[\"id\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Countries Polygons/Multipolygons\n",
    "\n",
    "gj_countries = get_countries_data()\n",
    "\n",
    "len(gj_countries[\"features\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = space.add_features(features=gj_countries)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get Statistics of a Space.\n",
    "\n",
    "space.get_statistics()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read all features from space:\n",
    "for feature in space.iter_feature():\n",
    "    print(feature)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get some feature from space\n",
    "test = space.get_features(feature_ids=[\"IND\",\"DEU\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create some new test data\n",
    "test['features'][0]['id'] = 'test1'\n",
    "test['features'][1]['id'] = 'test2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add a new feature in space\n",
    "_ = space.add_feature(feature_id='test1', data=test['features'][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get a feature from space\n",
    "space.get_feature(feature_id='test1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Update a feature in space\n",
    "_ = space.update_feature(feature_id='test1', data=test['features'][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Delete a feature in space\n",
    "space.delete_feature(feature_id='test1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add new features in space\n",
    "_ = space.add_features(features=test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get newly added features in space\n",
    "space.get_features(feature_ids=[\"test1\",\"test2\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Update features in space\n",
    "space.update_features(features=test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Delete the new fatures added in space.\n",
    "space.delete_features(feature_ids=[\"test1\",\"test2\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Search features in space\n",
    "for feature in space.search(params={\"p.name\": \"India\"}):\n",
    "    print(feature)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get features in a bounding box from space\n",
    "for feature in space.features_in_bbox(bbox=[0, 0, 20, 20]):\n",
    "    print(feature)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get features in tile from space.\n",
    "for feature in space.features_in_tile(tile_type=\"here\", tile_id=\"12\"):\n",
    "    print(feature)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get features in radius using spatial search in space.\n",
    "for feature in space.spatial_search(lat=37.377228699000057, lon=74.512691691000043, radius=100000):\n",
    "    print(feature)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get features which interesect the provided geometry.\n",
    "\n",
    "geom = {\"type\": \"Point\", \"coordinates\": [72.8557, 19.1526]}\n",
    "for feature in space.spatial_search_geometry(data=geom):\n",
    "    print(feature)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add features from csv file\n",
    "space.add_features_csv('data/test.csv', lat_col='latitude', lon_col='longitude', id_col='policyID')\n",
    "space.get_feature(feature_id='333743')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add features from geojson file\n",
    "space.add_features_geojson('data/test.geojson')\n",
    "space.get_feature(feature_id='test_geojson_1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Delete space\n",
    "space.delete()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "space.info"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
